$(document).ready(function(e) {
    $(".login").dialog({
		autoOpen: false,
		bgiframe: true,
		dialogClass:'login_close',
		closeOnEscape:false,
		hide:'slide(100)',
		height:250,
		modal:true,
		open:function(event,ui){fleshvcode();$('#username').focus();},
		//position:["center","center"],
		title:"登录",
		show:'slide(1000)',
		resizable:false,
		width:400
	});
	$('.login').keyup(function(e) {
		if(e.keyCode==13){
			login();
		}
    });
});
function login(){
	var username=/^[\w\d]{3,20}$/.exec($('#username').val())?$('#username').val():null;
	var password=$('#password').val();
	var vcode=/^[\w\d]{4}$/.exec($('#vcode').val())?$('#vcode').val():null;
	if(username==null){
		alert('请输入正确用户名！');
		$('#username').focus();
		return;
	}
	if(password==''){
		alert('请输入密码！');
		$('#password').focus();
		return;
	}
	if(vcode==null){
		alert('请输入正验证码！');
		$('#vcode').focus();
		return;
	}
	$.ajax({
		url:'/Login',
		contentType: "application/x-www-form-urlencoded;charset=utf-8",
		dataType:"JSON",
		cache:false,
		data:{username:username,password:md5(password),validate:vcode},
		success: function(rs){
			if(rs.code==0){
				$('.login').dialog('close');
				login_reset();
			}else{
				if(rs.code==1)
					fleshvcode();
				alert(rs.msg)
			}
		}
	});
}
function login_reset(){
	$('#username').val('');
	$('#password').val('');
	$('#vcode').val('');
	fleshvcode();
	$('#username').focus();
}
function fleshvcode(){
	$('#vcodeimg').attr('src','/validateCode?_='+Math.random());
	$('#vcode').val('');
	$('#vcode').focus();
}